package com.example.springboot.config;

import java.io.Serializable;

public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private boolean success = true; // 成功标志
    private boolean crypto = false; // 是否响应结果解密/ SM4/CBC
    private String message = ""; // 返回处理消息
    private Integer code = 0; // 返回代码
    private T result; // 返回数据对象
    private long timestamp = System.currentTimeMillis(); // 时间戳

    // 默认构造函数
    public Result() {}

    // 带参数的构造函数
    public Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    // 设置成功结果
    public Result<T> success(String message) {
        this.message = message;
        this.code = 200;
        this.success = true;
        return this;
    }

    // 创建成功结果
    public static <T> Result<T> OK() {
        return new Result<>(200, "操作成功");
    }

    // 创建成功结果，带数据
    public static <T> Result<T> OK(T data) {
        Result<T> result = OK();
        result.setResult(data);
        return result;
    }

    // 创建成功结果，带消息和数据
    public static <T> Result<T> OK(String msg, T data) {
        Result<T> result = OK();
        result.setMessage(msg);
        result.setResult(data);
        return result;
    }

    // 创建错误结果，带消息和数据
    public static <T> Result<T> error(String msg, T data) {
        return error(500, msg, data);
    }

    private static <T> Result<T> error(int i, String msg, T data) {
        Result<T> result = error(i, msg);
        result.setResult(data);
        return result;
    }

    // 创建错误结果，带消息
    public static <T> Result<T> error(String msg) {
        return error(500, msg);
    }

    // 创建错误结果，带状态码和消息
    public static <T> Result<T> error(int code, String msg) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(msg);
        result.setSuccess(false);
        return result;
    }

    // 设置 500 错误
    public Result<T> error500(String message) {
        return error(500, message);
    }

    // 设置未授权结果
    public static <T> Result<T> noAuth(String msg) {
        return error(510, msg);
    }

    // Getter 和 Setter 方法
    public boolean isSuccess() { return success; }
    public boolean isCrypto() { return crypto; }
    public String getMessage() { return message; }
    public Integer getCode() { return code; }
    public T getResult() { return result; }
    public long getTimestamp() { return timestamp; }

    public void setSuccess(boolean success) { this.success = success; }
    public void setCrypto(boolean crypto) { this.crypto = crypto; }
    public void setMessage(String message) { this.message = message; }
    public void setCode(Integer code) { this.code = code; }
    public void setResult(T result) { this.result = result; }
    public void setTimestamp(long timestamp) { this.timestamp = timestamp; }

    // equals 和 hashCode 方法
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Result)) return false;
        Result<?> other = (Result<?>) o;
        return success == other.success && crypto == other.crypto && timestamp == other.timestamp &&
                code.equals(other.code) && message.equals(other.message) && result.equals(other.result);
    }

    @Override
    public int hashCode() {
        return 31 * (success ? 1 : 0) +
                (crypto ? 1 : 0) +
                code.hashCode() +
                message.hashCode() +
                result.hashCode() +
                Long.hashCode(timestamp);
    }

    @Override
    public String toString() {
        return "Result{" +
                "success=" + success +
                ", crypto=" + crypto +
                ", message='" + message + '\'' +
                ", code=" + code +
                ", result=" + result +
                ", timestamp=" + timestamp +
                '}';
    }
}
